package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/cargo/contract")
public class ContractController  extends BaseController{

    @Reference
    private ContractService contractService;

    //分页查询购销合同列表
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") int page , @RequestParam(defaultValue = "10") int size){
        ContractExample example = new ContractExample();
        //查询条件
        //创建Criteria
        ContractExample.Criteria criteria =example.createCriteria();
        //指定条件
        criteria.andCompanyIdEqualTo(getLoginCompanyId());


        /**
         * 细粒度权限控制
         *      1.获取当前用户的级别
         *      2.根据不用用户的级别添加不同的查询条件
         */
        User user = getLoginUser();
        Integer degree = user.getDegree();
        if (degree == 4){ //普通员工
            criteria.andCreateByEqualTo(user.getId());
        }else if(degree == 3){//管理本部门(经理：部门id)
            criteria.andCreateDeptEqualTo(user.getDeptId());
        }else if (degree == 2){//管理所有下属部门和人员
            criteria.andCreateDeptLike(user.getDeptId()+"%");
        }
        //1.企业管理员

        //根据create_time desc
        example.setOrderByClause("create_time desc");
        //分页查询
        PageInfo info = contractService.findAll(example,page,size);
        //存入request域
        request.setAttribute("page",info);
        //跳转页面
        return "/cargo/contract/contract-list";
    }

    //跳转添加合同页面  进行回显
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "/cargo/contract/contract-add";
    }
    //保存 或 更新
/*    @RequestMapping("/edit")
    public String edit(Contract contract){
        contract.setCompanyId(getLoginCompanyId());
        contract.setCompanyName(getLoginCompanyName());

        if (StringUtils.isEmpty(contract.getId())){
            //进行保存
            contractService.save(contract);
        }else{
            //进行更新
            contractService.update(contract);
        }
        return "redirect:/cargo/contract/list.do";
    }*/
   @RequestMapping(value="/edit")
   public String edit(Contract contract) {
       contract.setCompanyId(getLoginCompanyId());
       contract.setCompanyName(getLoginCompanyName());
       if(StringUtils.isEmpty(contract.getId())) {
           //保存购销合同(设置创建人和创建人部门)
           //1.获取当前登录用户
            User user =getLoginUser();
           //2.设置创建人和部门
           contract.setCreateBy(user.getId());
           contract.setCreateDept(user.getDeptId());

           contractService.save(contract);
       }else{
           contractService.update(contract);
       }
       return "redirect:/cargo/contract/list.do";
   }
    //跳转更新页面 回显
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        Contract contract = contractService.findById(id);
        request.setAttribute("contract",contract);
        return "/cargo/contract/contract-update";
    }
    //删除
    @RequestMapping("/delete")
    public String delete(String id){
        contractService.delete(id);
        return "redirect:/cargo/contract/list.do";
    }

    //跳转查看页面
    @RequestMapping("/toView")
    public String toView(String id){
        Contract contract = contractService.findById(id);
        request.setAttribute("contract",contract);
        return "/cargo/contract/contract-view";
    }




    //提交
    @RequestMapping(value = "/submit")
    public String submit(String id){
       //将合同状态改为1
        Contract contract = new Contract();
        contract.setId(id);
        contract.setState(1);
        contractService.update(contract);
        return "redirect:/cargo/contract/list.do";
    }
    //取消提交
    @RequestMapping("/cancel")
    public String cancel(String id){
       //将合同状态改为0
        Contract contract = new Contract();
        contract.setId(id);
        contract.setState(0);
        contractService.update(contract);
        return "redirect:/cargo/contract/list.do";
    }


}
